package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.system.domain.SysPost;
import com.hxzy.system.domain.dto.SysPostDTO;
import com.hxzy.system.mapper.SysPostMapper;
import com.hxzy.system.service.ISysPostService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author： 17602
 * @Date： 2022/9/6 11:43
 * @Desc： 岗位
 **/
@Service
public class ISysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {


    @Override
    public IPage<SysPost> selectPostList (SysPostDTO post) {
        Page<SysPost> page = PageDTO.of(post.getPageNum(), post.getPageSize());
        return this.baseMapper.selectPostList(page,post);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll() {
        return this.baseMapper.selectPostAll();
    }

    /**
     * 为用户授权岗位
     *
     * @param userId
     * @param
     */
    @Override
    public void grantPostForUser(Long userId, List<Long> postIds) {
        int delete = this.baseMapper.deleteByUserId(userId);
        if (delete <= 0) {
            throw new ServiceException("执行出错");
        }
        for (Long postId : postIds) {
            this.baseMapper.insertPost(userId, postId);
        }
    }

    /**
     * 根据用户id查询其岗位
     *
     * @param id
     * @return
     */
    @Override
    public List<SysPost> selectPostByUserId (Long id) {
        return this.baseMapper.selectPostByUserId(id);
    }


    @Override
    public boolean checkPostNameUnique (String postName) {
        int i = this.baseMapper.checkPostNameUnique(postName);
        return i > 0;
    }

    @Override
    public boolean checkPostCodeUnique (String code) {
        int i = this.baseMapper.checkPostCodeUnique(code);
        return i > 0;
    }

    @Override
    public boolean selectPostAndUserById (Long id) {
        int i = this.baseMapper.selectPostAndUserById(id);
        return i > 0;
    }

    @Override
    public List<SysPost> searchExport (SysPostDTO sysPostDTO) {
        return this.baseMapper.searchExport(sysPostDTO);
    }
}
